#include <cmath>
#include "gui/objects/segments/GUIRoadSegment.h"
#include "util/MathUtils.h"

namespace gui {

GUIRoadSegment::GUIRoadSegment(RoadSegment* roadSegment) {
	Intersection* origin = roadSegment->getOrigin();
	Intersection* destination = roadSegment->getDestination();
	this->setCenterInMeters((origin->getX() + destination->getX()) / 2.0, (origin->getY() + destination->getY()) / 2.0);
	this->setSizeInMeters(0.0, this->getRoadSegmentLength(origin, destination));
	this->setRotation(roadSegment->getAngle());
}

GUIRoadSegment::GUIRoadSegment(double width, MapPoint* origin, MapPoint* destination) {
	this->setDimensionsAndLocation(width, origin, destination);
}

GUIRoadSegment::~GUIRoadSegment() { }

double GUIRoadSegment::getRoadSegmentLength(MapPoint* o, MapPoint* d) {
	double x = d->getX() - o->getX();
	double y = d->getY() - o->getY();
	return sqrt(x * x + y * y);
}

void GUIRoadSegment::setDimensionsAndLocation(double width, MapPoint* origin, MapPoint* destination) {
       this->setCenterInMeters((origin->getX() + destination->getX()) / 2.0, (origin->getY() + destination->getY()) / 2.0);

       this->setSizeInMeters(width, this->getRoadSegmentLength(origin, destination));
       double angle = atan2((destination->getY() - origin->getY()) , (destination->getX() - origin->getX())) + PI / 2;
       this->setRotation(angle);
}

}
